import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import cache from '@/utils/cache'
import UserLogin from '@/views/Users/UserLogin.vue'
import WebIndex from '@/views/Index/WebIndex.vue'
import QuickEntry from '@/views/Index/QuickEntry.vue'
import QuickView from '@/views/Index/QuickView.vue'
import GiftRanking from '@/views/Index/GiftRanking.vue'
import WordCloud from '@/views/Index/WordCloud.vue'
import BarrageParsing from '@/views/Index/BarrageParsing.vue'
import GiftParsing from '@/views/Index/GiftParsing.vue'
import DetailParsing from '@/views/Index/DetailParsing.vue'
import WebAdmin from '@/views/Admin/WebAdmin.vue'
import RoomManage from '@/views/Admin/RoomManage.vue'
import GiftManage from '@/views/Admin/GiftManage.vue'
import GiftRecordManage from '@/views/Admin/GiftRecordManage.vue'
import BarrageManage from '@/views/Admin/BarrageManage.vue'
const routes: Array<RouteRecordRaw> = [
  {
    // 主页默认重定向到 快捷入口
    path: '/',
    redirect: '/index/quickView'
  },
  {
    // 主页默认重定向到 快捷入口
    path: '/index',
    name: 'index',
    redirect: '/index/quickView',
    component: WebIndex,
    children: [
      {
        // 速览
        path: 'quickView',
        component: QuickView
      },
      {
        // 快捷入口 废弃
        path: 'quickEntry',
        component: QuickEntry
      },
      {
        // 礼物排名
        path: 'giftRanking',
        component: GiftRanking
      },
      {
        // 弹幕词云 弹幕热词
        path: 'wordCloud',
        component: WordCloud
      },
      {
        // 弹幕分析
        path: 'detailParsing',
        component: DetailParsing
      },
      {
        // 弹幕分析
        path: 'barrageParsing',
        component: BarrageParsing
      },
      {
        // 礼物分析
        path: 'giftParsing',
        component: GiftParsing
      },
      {
        // 管理
        path: 'admin',
        redirect: '/admin'
      }
    ]
  },
  {
    // 登录
    path: '/login',
    name: 'login',
    component: UserLogin
  },
  {
    // 管理
    path: '/admin',
    name: 'admin',
    component: WebAdmin,
    redirect: '/admin/roomManage',
    children: [
      {
        // 直播间管理
        path: 'roomManage',
        component: RoomManage
      },
      {
        // 礼物管理
        path: 'giftManage',
        component: GiftManage
      },
      {
        // 礼物记录管理
        path: 'GiftRecordManage',
        component: GiftRecordManage
      },
      {
        // 弹幕记录管理
        path: 'barrageManage',
        component: BarrageManage
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  if ((to.name === 'admin' || to.path === '/admin') && !cache.cache('token')) {
    next({ name: 'login' })
  } else {
    next()
  }
})
export default router
